package org.infosys.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.infosys.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallbackMethod")
    @GetMapping("/order/{id}")
    public User getById(@PathVariable("id") Long id) {

        //String url = "http://localhost:1020/user/"+id;
        String url = "http://user-server/user/"+id;

        //发送http请求
        return restTemplate.getForObject(url, User.class);
    }

    public User fallbackMethod(@PathVariable("id") Long id) {
        return User.builder().id(-1L).intro("降级了").build();
    }
}
